package com.rem.shiro.config;

import com.rem.shiro.pojo.User;

/**
 * 本地缓存
 *
 * @author Rem
 * @date 2021/4/16
 * jdk1.7之后，实现AutoCloseable接口，可自动释放资源，不用手动释放。
 */
public class MyContext implements AutoCloseable {

    private static final ThreadLocal<User> USER = new ThreadLocal<>();

    public MyContext() {
    }

    public MyContext(User user) {
        if (user != null) {
            USER.set(user);
        }
    }


    @Override
    public void close() {
        USER.remove();
    }

    public static User getUser() {
        return USER.get();
    }

}